<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: GetCircle.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\note;

use app\api\model\dao\DashangGift;
use app\api\model\dao\DashangRecord;
use app\api\model\dao\Note;
use app\api\model\dao\NotePraise;
use app\library\Error;
use app\library\Flag;
use app\library\PageBase;
use think\Exception;

class GetRewardGift extends PageBase
{

    private $objNote;
    private $objNotePraise;
    private $objDashangGift;
    private $objDashangRecord;

    public function __construct()
    {
        parent::__construct();

        $this->objNote = new Note();
        $this->objNotePraise = new NotePraise();
        $this->objDashangGift = new DashangGift();
        $this->objDashangRecord = new DashangRecord();
    }

    /**
     * page 入口
     *
     * @param $param
     * @return array
     * @throws Exception
     */
    public function myExcute($param)
    {
        // 2、从数据库中获取帖子详情
        $ret = $this->getGiftFromDB();
        if (false === $ret) {
            throw new Exception('get dashang gift from db fail.', Error::ERR_SYS);
        }

        $this->data = $this->composeData($ret);
    }

    /**
     * 从数据库中获取打赏礼物
     *
     * @param $cir_id
     * @return bool|false|\PDOStatement|string|\think\Collection
     */
    public function getGiftFromDB()
    {
        $arrCond = [
            //'up' => ['=', Flag::CONST_DASHANGGIFT_UP]  // 先注释了，等后台吧上架功能加上了，在恢复
        ];
        $ret = $this->objDashangGift->getDashangGift($arrCond, ['id', 'name', 'pic', 'popularity', 'money']);
        if (false === $ret) {
            return false;
        }
        if (empty($ret)) {
            return [];
        }
        return $ret;
    }

    public function composeData($gift)
    {
        if (empty($gift)) {
            return [];
        }
        $data = [];
        foreach ($gift as $val) {
            $data[] = [
                'gift_id' => $val['id'],
                'gift_name' => $val['name'],
                'pic' => $val['pic'],
                'popularity' => $val['popularity'],
                'money' => $val['money'],
            ];
        }
        return $data;
    }
}
